<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('项目收款情况汇总')" />
</head>
<body class="gray-bg" onload="init()">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row" style="height:75%">
            <div class="col-sm-6">
                <div class="ibox">
                    <div class="ibox-content" id="totalDiv">
                    </div>
                </div>
                <div class="ibox">
                    <div class="ibox-title">
                        <h5>当前日期所在周、月、季度预测的回款总额</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="graph_flot.html#">选项1</a></li>
                                <li><a href="graph_flot.html#">选项2</a></li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <table class="table table-stripped m-t-md" style="border-top:none">
                            <tbody id="predict">

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox">
                    <div class="ibox-title">
                        <h5>本年度预算收入完成率 = 当前公司实际回款数 / 公司全年预算</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="graph_flot.html#">选项1</a></li>
                                <li><a href="graph_flot.html#">选项2</a></li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="echarts" id="statistic8" style="height:380px;"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="ibox">
                    <div class="ibox-content">
                        <table class="table table-stripped m-t-md">
                            <thead>
                            <tr>
                                <th >合同总览</th>
                                <th >涉及笔数</th>
                                <th>涉及金额</th>
                            </tr>
                            </thead>
                            <tbody id="contractOverview">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox">
                    <div class="ibox-content">
                        <H3 style="float:left;width:fit-content;">逾期合同TOP10（当前日期大于合同任一阶段预测完成时间均为逾期）</H3>
                        <a style="float:right;" onclick="overdueContract()">更多</a>
                        <table class="table table-stripped m-t-md">
                            <thead>
                            <tr>
                                <th >合同名</th>
                                <th >逾期时间</th>
                                <th>应收金额</th>
                                <th>销售负责人</th>
                                <th>交付负责人</th>
                            </tr>
                            </thead>
                            <tbody id="overdue">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="row" style="height:75%">
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>项目收款汇总表</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="graph_flot.html#">选项1</a>
                                </li>
                                <li><a href="graph_flot.html#">选项2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="echarts" id="statistic1" style="height:380px;"></div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>销售人员合同额占比</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="graph_flot.html#">选项1</a>
                                </li>
                                <li><a href="graph_flot.html#">选项2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="echarts" id="statistic4" style="height:380px;"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row" style="height:75%">
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>预算预测实际收款月度走势</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="graph_flot.html#">选项1</a></li>
                                <li><a href="graph_flot.html#">选项2</a></li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="echarts" id="statistic3" style="height:380px;"></div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>预算预测实际收款季度表</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="graph_flot.html#">选项1</a></li>
                                <li><a href="graph_flot.html#">选项2</a></li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="echarts" id="statistic2" style="height:380px;"></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row" style="height:75%">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>预算预测实际收款周表</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="graph_flot.html#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="graph_flot.html#">选项1</a></li>
                                <li><a href="graph_flot.html#">选项2</a></li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="echarts" id="statistic7" style="height:380px;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: echarts-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/yingshou";
        function init(){
            initStatistic1();
            initStatistic2();
            initStatistic3();
            initStatistic4();
            initStatistic5();
            initStatistic6();
            initStatistic7();
            initStatistic8();
        }
        function initStatistic1(){
            // 基于准备好的dom，初始化echarts实例
            var statistic1 = echarts.init(document.getElementById('statistic1'));
            $.ajax({
                type: "GET",
                url: prefix + "/statistic1",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        var obj = JSON.parse(result.msg);
                        var contractNames = [];
                        var sumActuals = [];
                        var others = [];
                        for (let index in obj) {
                            contractNames[index] = obj[index].contractName;
                            sumActuals[index] = obj[index].sumActual ? obj[index].sumActual : 0;
                            others[index] = obj[index].booking - sumActuals[index];
                        }
                        var option = {
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                                }
                            },
                            legend: {
                                right: '5%',
                                data: ['已收款', '待收款']
                            },
                            grid: {
                                left: '3%',
                                right: '4%',
                                bottom: '3%',
                                containLabel: true
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    data: contractNames,
                                    axisLabel: {
                                        interval:0,
                                        rotate:40
                                    }

                                }
                            ],
                            yAxis: [
                                {
                                    type: 'value'
                                }
                            ],
                            series: [
                                {
                                    name: '已收款',
                                    type: 'bar',
                                    stack: '收款情况汇总',
                                    data: sumActuals
                                },
                                {
                                    name: '待收款',
                                    type: 'bar',
                                    stack: '收款情况汇总',
                                    data: others
                                }
                            ]
                        };
                        // 使用刚指定的配置项和数据显示图表。
                        statistic1.setOption(option);
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }

        function initStatistic2(){
            // 基于准备好的dom，初始化echarts实例
            var statistic2 = echarts.init(document.getElementById('statistic2'));
            $.ajax({
                type: "GET",
                url: prefix + "/statistic2",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        var obj = JSON.parse(result.msg);
                        var quaters = [];
                        var budgets = [];
                        var preDicts = [];
                        var actuals = [];
                        for(var p in obj){//遍历json对象的每个key/value对,p为key
                            quaters.push(p); //季度数组
                            budgets.push(obj[p][0]); //预算数组
                            preDicts.push(obj[p][1]); //预测数组
                            actuals.push(obj[p][2]); //实际数组
                        }
                        option = {
                            color: ['#003366', '#4cabce', '#e5323e'],
                            legend: {
                                right: '5%',
                                data: ['预算金额', '预测金额', '实际金额']
                            },
                            tooltip: {
                                trigger: 'axis',
                                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                                }
                            },
                            grid: {
                                left: '3%',
                                right: '4%',
                                bottom: '3%',
                                containLabel: true
                            },
                            xAxis: [
                                {
                                    type: 'category',
                                    data: quaters,
                                    axisTick: {
                                        alignWithLabel: true
                                    }
                                }
                            ],
                            yAxis: [
                                {
                                    type: 'value'
                                }
                            ],
                            series: [
                                {
                                    name: '预算金额',
                                    type: 'bar',
                                    data: budgets
                                },
                                {
                                    name: '预测金额',
                                    type: 'bar',
                                    data: preDicts
                                },
                                {
                                    name: '实际金额',
                                    type: 'bar',
                                    data: actuals
                                }
                            ]
                        };
                        statistic2.setOption(option);
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }

        function initStatistic3(){
            // 基于准备好的dom，初始化echarts实例
            var statistic3 = echarts.init(document.getElementById('statistic3'));
            $.ajax({
                type: "GET",
                url: prefix + "/statistic3",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        var obj = JSON.parse(result.msg);
                        var months = [];
                        var budgets = [];
                        var preDicts = [];
                        var actuals = [];
                        for(var p in obj){//遍历json对象的每个key/value对,p为key
                            months.push(p); //月数组
                            budgets.push(obj[p][0]); //预算数组
                            preDicts.push(obj[p][1]); //预测数组
                            actuals.push(obj[p][2]); //实际数组
                        }
                        option = {

                            tooltip: {
                                trigger: 'axis'
                            },
                            legend: {
                                right: '5%',
                                data: ['预算金额', '预测金额', '实际金额']
                            },
                            grid: {
                                left: '3%',
                                right: '4%',
                                bottom: '3%',
                                containLabel: true
                            },
                            toolbox: {
                                feature: {
                                    //saveAsImage: {}
                                }
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: months
                            },
                            yAxis: {
                                type: 'value'
                            },
                            series: [
                                {
                                    name: '预算金额',
                                    type: 'line',
                                    data: budgets
                                },
                                {
                                    name: '预测金额',
                                    type: 'line',
                                    data: preDicts
                                },
                                {
                                    name: '实际金额',
                                    type: 'line',
                                    data: actuals
                                }
                            ]
                        };
                        statistic3.setOption(option);
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }

        function initStatistic4(){
            // 基于准备好的dom，初始化echarts实例
            var statistic4 = echarts.init(document.getElementById('statistic4'));
            $.ajax({
                type: "GET",
                url: prefix + "/statistic4",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        var obj = JSON.parse(result.msg);
                        var bookings = [];
                        var names = [];
                        for (let index in obj) {
                            let dataObj = new Object();
                            dataObj.value = obj[index].booking;
                            dataObj.name = obj[index].xsPrincipalName;
                            bookings.push(dataObj);
                            names.push(obj[index].xsPrincipalName);
                        }
                        option = {

                            tooltip: {
                                trigger: 'item',
                                formatter: '{a} <br/>{b} : {c} ({d}%)'
                            },
                            legend: {
                                orient: 'vertical',
                                right: '5%',
                                data: names
                            },
                            series: [
                                {
                                    name: '销售人员合同',
                                    type: 'pie',
                                    radius: '55%',
                                    center: ['50%', '60%'],
                                    data: bookings,
                                    emphasis: {
                                        itemStyle: {
                                            shadowBlur: 10,
                                            shadowOffsetX: 0,
                                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                                        }
                                    }
                                }
                            ]
                        };
                        statistic4.setOption(option);
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }

        function initStatistic5(){
            let totalDiv = $('#totalDiv');
            let contractOverview = $('#contractOverview');
            $.ajax({
                type: "GET",
                url: prefix + "/statistic5",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        var obj = JSON.parse(result.msg);
                        var mztjyhk = obj[0].contractNumber/10000;
                        totalDiv.html('<h1>'+ obj[0].showType + ':<a src="#" onclick="receivables()">' + mztjyhk.toFixed(2) + '</a>万元<span style="font-size:12px;">（涉及合同数：<a src="#" onclick="receivables()">' + obj[0].contractCount + '</a>笔）</span></h1><div class="stat-percent font-bold text-navy">43%<i class="fa fa-bolt"></i></div><small>占总收入比重</small>');
                        contractOverview.append('<tr><td>' + obj[2].showType + '</td><td>' + obj[2].contractCount + '</td><td>' + obj[2].contractNumber + '</td></tr>');
                        contractOverview.append('<tr><td>' + obj[3].showType + '</td><td><a src="#" onclick="incomplete()">' + obj[3].contractCount + '</a></td><td><a src="#" onclick="incomplete()">' + obj[3].contractNumber + '</a></td></tr>');
                        contractOverview.append('<tr><td>' + obj[4].showType + '</td><td>' + obj[4].contractCount + '</td><td>' + obj[4].contractNumber + '</td></tr>');
                        contractOverview.append('<tr><td>' + obj[5].showType + '</td><td>' + obj[5].contractCount + '</td><td>' + obj[5].contractNumber + '</td></tr>');
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }

        function initStatistic6(){
            let overdue = $('#overdue');
            $.ajax({
                type: "GET",
                url: prefix + "/statistic6",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        let obj = JSON.parse(result.msg);
                        for (let i = 0; i < obj.length; i++) {
                            var days = obj[i].overDueDays;
                            var years = Math.floor(days/365)<1?'<1年':Math.floor(days/365)+'年';
                            overdue.append('<tr><td>' + obj[i].contractName + '</td><td>' + obj[i].overDueDays + '天(' + years + ')</td><td>' + obj[i].overDueMoney + '</td><td>' + obj[i].xsPrincipalName + '</td><td>' + obj[i].jfPrincipalName + '</td></tr>');
                        }
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }

        function initStatistic7(){
            // 基于准备好的dom，初始化echarts实例
            var statistic7 = echarts.init(document.getElementById('statistic7'));
            $.ajax({
                type: "GET",
                url: prefix + "/statistic7",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        var obj = JSON.parse(result.msg);
                        var weeks = [];
                        var budgets = [];
                        var preDicts = [];
                        var actuals = [];
                        for(var p in obj){//遍历json对象的每个key/value对,p为key
                            weeks.push(p); //周数组
                            budgets.push(obj[p][0]); //预算数组
                            preDicts.push(obj[p][1]); //预测数组
                            actuals.push(obj[p][2]); //实际数组
                        }
                        option = {
                            tooltip: {
                                trigger: 'axis'
                            },
                            legend: {
                                right: '5%',
                                data: ['预算金额', '预测金额', '实际金额']
                            },
                            grid: {
                                left: '3%',
                                right: '4%',
                                bottom: '3%',
                                containLabel: true
                            },
                            toolbox: {
                                show: true,
                                orient: 'vertical',
                                left: 'right',
                                top: 'center',
                                feature: {
                                    mark: {show: true},
                                    dataView: {show: true, readOnly: false},
                                    magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                                    restore: {show: true},
                                    saveAsImage: {show: true}
                                }
                            },
                            xAxis: {
                                type: 'category',
                                boundaryGap: false,
                                data: weeks
                            },
                            yAxis: {
                                type: 'value'
                            },
                            series: [
                                {
                                    name: '预算金额',
                                    type: 'line',
                                    data: budgets
                                },
                                {
                                    name: '预测金额',
                                    type: 'line',
                                    data: preDicts
                                },
                                {
                                    name: '实际金额',
                                    type: 'line',
                                    data: actuals
                                }
                            ]
                        };
                        statistic7.setOption(option);
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }

        //本周计划汇款、本月计划回款、本季计划回款、本年度预算收入完成率
        function initStatistic8(){
            // 基于准备好的dom，初始化echarts实例
            let predict = $('#predict');
            let statistic8 = echarts.init(document.getElementById('statistic8'));
            $.ajax({
                type: "GET",
                url: prefix + "/statistic8",
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        let obj = JSON.parse(result.msg);
                        let bzycze = obj['本周预测总额'] ? obj['本周预测总额'] : 0;
                        bzycze = (bzycze / 10000).toFixed(2)
                        let byycze = obj['本月预测总额'] ? obj['本月预测总额'] : 0;
                        byycze = (byycze / 10000).toFixed(2)
                        let bjycze = obj['本季预测总额'] ? obj['本季预测总额'] : 0;
                        bjycze = (bjycze / 10000).toFixed(2)

                        let zClass = ['text-navy','fa-level-up'];
                        let yClass = ['text-danger','fa-level-down'];
                        let jClass = ['text-navy','fa-level-up'];
                        let zUpDown = '提高';
                        let yUpDown = '降低';
                        let jUpDown = '提高';

                        predict.append('<tr>');
                        predict.append('<td><h5>本周计划回款(万元)</h5><h1 class="no-margins">' + bzycze + '</h1><div class="stat-percent font-bold ' + zClass[0] + '">11% <i class="fa ' + zClass[1] + '"></i></div><small style="float:right">较上周' + zUpDown + '</small></td>');
                        predict.append('<td><h5>本月计划回款(万元)</h5><h1 class="no-margins">' + byycze + '</h1><div class="stat-percent font-bold ' + yClass[0] + '">11% <i class="fa ' + yClass[1] + '"></i></div><small style="float:right">较上周' + yUpDown + '</small></td>');
                        predict.append('<td><h5>本季度计划回款(万元)</h5><h1 class="no-margins">' + bjycze + '</h1><div class="stat-percent font-bold ' + jClass[0] + '">11% <i class="fa ' + jClass[1] + '"></i></div><small style="float:right">较上周' + jUpDown + '</small></td>');
                        predict.append('</tr>');
                        let bnysze = obj['本年预算总额'];
                        let bnsjze = obj['本年实际总额'];
                        option = {
                            tooltip: {
                                formatter: '{a} <br/>  {c}万元'
                            },
                            toolbox: {
                                feature: {
                                    // restore: {},
                                    // saveAsImage: {}
                                }
                            },
                            series: [
                                {
                                    name: '已收款',
                                    type: 'gauge',
                                    max: bnysze,
                                    detail: {
                                        formatter: function(value){
                                            let result = value/bnysze;
                                            result = result.toFixed(2);
                                            return result + '%';
                                        }
                                    },
                                    axisLabel: {
                                        distance: -80,
                                        fontSize: 10,
                                        formatter: function(v){
                                            return (v/10000).toFixed(2) + '万元';
                                        }
                                    },
                                    data: [{value: bnsjze, name: '完成率'}]
                                }
                            ]
                        };
                        statistic8.setOption(option);
                    } else {
                        console.log(result.code, result.msg);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        }


        function receivables(){
            var options = {
                title: '应收合同详情',
                width: "960",
                url: prefix + "/receivables"
            };
            $.modal.openOptions(options);
        }

        function incomplete(){
            var options = {
                title: '执行中合同详情',
                width: "960",
                url: prefix + "/incomplete"
            };
            $.modal.openOptions(options);
        }

        function overdueContract(){
            var options = {
                title: '逾期合同详情',
                width: "960",
                url: prefix + "/overdueContract"
            };
            $.modal.openOptions(options);
        }
    </script>
</body>
</html>